package com.huawei.esdk.tp.domain.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.MessageContext;
import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.constants.ESDKErrorCodeConstant;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.platform.nemgr.itf.IDeviceManager;
import com.huawei.esdk.tp.authorize.SessionManager;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.common.constants.InfoKeys;
import com.huawei.esdk.tp.common.constants.TPConstant;
import com.huawei.esdk.tp.device.oa.IADCapability;
import com.huawei.esdk.tp.device.smc.ISiteManagerCapability;
import com.huawei.esdk.tp.device.smc.v100r003c10.SMC20V1R3C10ISiteManagerCapability;
import com.huawei.esdk.tp.device.terminal.TerminalServiceItf;
import com.huawei.esdk.tp.domain.model.bean.ConfHistory;
import com.huawei.esdk.tp.domain.model.bean.ControlPwd;
import com.huawei.esdk.tp.domain.model.bean.DateTimeRange;
import com.huawei.esdk.tp.domain.model.bean.FreeBusyInfo;
import com.huawei.esdk.tp.domain.model.bean.MTStruct;
import com.huawei.esdk.tp.domain.model.bean.PagedList;
import com.huawei.esdk.tp.domain.model.bean.PreConf;
import com.huawei.esdk.tp.domain.model.bean.QueryCondition;
import com.huawei.esdk.tp.domain.model.bean.QuerySites;
import com.huawei.esdk.tp.domain.model.bean.SiteFreeBusyRes;
import com.huawei.esdk.tp.domain.model.bean.SortField;
import com.huawei.esdk.tp.domain.model.bean.VideoCapbilityItem;

public class Site
{
    private static final Logger LOGGER = Logger.getLogger(Site.class);

    private TerminalServiceItf terminalService;

    private String uri;

    private String name;

    private String siteType;

    private String dialingMode;

    private List<FreeBusyInfo> freeBusyInfoList;

    private Camera camera;

    private StreamOperator streamOperator;

    private Integer status;

    private String model;

    private String lisence;

    private String softVersion;

    private String hardVersion;

    private String logicVersion;

    private Map<String, String> micVersion;

    private String from;

    private String rate;

    private Integer videoFormat;

    private Integer videoProtocol;

    //C30新增参数
    private Integer isLocalVideoOpen;
    
    // R5C00新增参数
    private String participantType;

    private Integer isLockVideoSource;
    
    //H.323、SIP协议会场注册SC的用户名
    private String regUser;
    //H.323、SIP协议会场注册SC的密码
    private String regPassword;
    //终端支持的视频能力参数列表。如果不填，则认为支持所有视频
    private List<VideoCapbilityItem> videoCapbilitys;
    
    private String orgId;
    
    private Integer type;
    
    public Integer getType()
    {
        return type;
    }

    public void setType(Integer type)
    {
        this.type = type;
    }

    public String getOrgId()
    {
        return orgId;
    }

    public void setOrgId(String orgId)
    {
        this.orgId = orgId;
    }

    public String getParticipantType()
    {
        return participantType;
    }

    public void setParticipantType(String participantType)
    {
        this.participantType = participantType;
    }
    
    public Integer getIsLockVideoSource()
    {
        return isLockVideoSource;
    }

    public void setIsLockVideoSource(Integer isLockVideoSource)
    {
        this.isLockVideoSource = isLockVideoSource;
    }

    public String getRate()
    {
        return rate;
    }

    public void setRate(String rate)
    {
        this.rate = rate;
    }



    public Integer getStatus()
    {
        return status;
    }

    public void setStatus(Integer status)
    {
        this.status = status;
    }

    protected static final IDeviceManager deviceManager = (IDeviceManager) ApplicationContextUtil
            .getBean("deviceManager");

    public Site()
    {
    }

    public Site(String uri)
    {
        this.uri = uri;
    }

    public static SDKResult<PagedList<Site>> querySites(QuerySites querySites)
            throws SDKException
    {
        ISiteManagerCapability siteManager = deviceManager
                .getDeviceServiceProxy(TPConstant.DEV_ID_SMC,
                        ISiteManagerCapability.class);
        return siteManager.querySites(querySites);
    }
    
    public StreamOperator getStreamOperator() throws SDKException
    {
        if (this.streamOperator == null)
        {
            this.streamOperator = new StreamOperator();
            streamOperator.setTerminalService(getTerminalClient(this.uri));
        }
        return streamOperator;
    }

    public void setStreamOperator(StreamOperator streamOperator)
    {
        this.streamOperator = streamOperator;
    }

    public void setCamera(Camera camera)
    {
        this.camera = camera;
    }

    public Camera getCamera() throws SDKException
    {
        if (this.camera == null)
        {
            this.camera = new Camera();
            camera.setTerminalService(getTerminalService());
        }
        return camera;
    }

    /**
     * 查询指定会场在指定时间范围内的忙闲状态。支持同时查询多个会场。
     * @throws SDKException
     */
    public SiteFreeBusyRes querySiteFreeBusy(List<String> siteUris,
            DateTimeRange timeRange) throws SDKException
    {
        ISiteManagerCapability siteManager = deviceManager
                .getDeviceServiceProxy(TPConstant.DEV_ID_SMC,
                        ISiteManagerCapability.class);
        return siteManager.querySiteFreeBusy(siteUris, timeRange);
    }

    /**
     * 查询指定会场在指定时间段内发生忙闲状态变化的数据
     * @throws SDKException
     */
    public SiteFreeBusyRes synchSiteFreeBusy(List<String> siteUris,
            DateTimeRange timeRange) throws SDKException
    {
        ISiteManagerCapability siteManager = deviceManager
                .getDeviceServiceProxy(TPConstant.DEV_ID_SMC,
                        ISiteManagerCapability.class);
        return siteManager.synchSiteFreeBusy(siteUris, timeRange);
    }
    
    public SDKErrorCode deleteSiteInfoEx(List<String> uris) throws SDKException
    {
        SMC20V1R3C10ISiteManagerCapability siteManager = deviceManager
            .getDeviceServiceProxy(TPConstant.DEV_ID_SMC,
                SMC20V1R3C10ISiteManagerCapability.class);
    
        return siteManager.deleteSiteInfo(uris);
    }

    /**
     * 主席会场循环观看、停止循环观看。只有主席才能进行该操作。
     */
    public SDKErrorCode autoView(String action, int time, String type,
            List<MTStruct> mtStruct)
    {
        // 1. Result should be refined
        // 2. Request parameter
        return null;
    }

    /**
     * 会议控制中添加临时会场。只有主席才能进行该操作。
     */
    public SDKErrorCode confAddTempSite(TempSite tempSite)
    {
        // 1. Result should be refined
        return null;
    }

    /**
     * 主席延长会议。
     */
    public SDKErrorCode confTimeDelay(int delayTime)
    {
        // 1. Result should be refined
        // 2. Request parameter
        return null;
    }

    /**
     * 查询当前会议中是否存在密码。
     */
    public SDKResult<ControlPwd> getConfCtrlPsd() throws SDKException
    {
        SDKResult<ControlPwd> result = getTerminalService().getConfCtrlPsd();
        return result;
    }

    /**
     * 获取会议历史记录列表。历史会议记录，只包括本地会场召集的多点会议，不包括点对点的通话记录。
     */
    public ConfHistory getConfRecordList()
    {
        // 1. Response should be refined
        return null;
    }

    /**
     * 获取预定义会议列表。
     */
    public PreConf getDefinedConfList()
    {
        // 1. Response should be refined
        return null;
    }

    /**
     * 远端有来电时，本地会场接听或挂断来电。当同时有多路来电时，可以选择接听或挂断其中的某一路或某几路。
     */
    public void incomingCallProc()
    {

    }

    /**
     * 查询会议信息(包含会场列表)
     */
    public void initSiteListData()
    {

    }

    /**
     * 查询24小时之内预约会议和已召开会议列表
     */
    public void inqueryConfList()
    {

    }

    /**
     * 用户可以查询终端当前系统是否处于休眠状态
     */
    public void isSystemSleep()
    {

    }

    /**
     * 申请主席。
     */
    public SDKErrorCode requestChair() throws SDKException
    {
        SDKErrorCode result = getTerminalService().requestChair();
        return result;
    }

    /**
     * 释放主席。
     */
    public SDKErrorCode releaseChair() throws SDKException
    {
        SDKErrorCode result = getTerminalService().releaseChair();
        return result;
    }

    /**
     * 申请发言。
     */
    public void requestFloor()
    {

    }

    /**
     * 确认会控密码，如果密码正确，则返回成功失败响应，密码错误就返回失败响应。
     */
    public void sendConfCtrlPsd()
    {

    }

    /**
     * 屏幕选看
     */
    public void viewScreen()
    {

    }

    public static SDKResult<PagedList<Site>> queryAllManagedSites(
            List<QueryCondition> filters, List<SortField> sortFields,
            int recordPerPage, int curPageNo)
    {
        // QuerySites
        return new SDKResult<PagedList<Site>>();
    }

    public SDKResult<Site> querySiteVersionInfo() throws SDKException
    {
        SDKResult<Site> result = null;
        try
        {
            result = getTerminalService().querySiteVersionInfo();
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("", e);
            result = new SDKResult<Site>();
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        return result;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getUri()
    {
        return uri;
    }

    public void setUri(String uri)
    {
        this.uri = uri;
    }

    public String getSiteType()
    {
        return siteType;
    }

    public void setSiteType(String siteType)
    {
        this.siteType = siteType;
    }

    public String getDialingMode()
    {
        return dialingMode;
    }

    public void setDialingMode(String dialingMode)
    {
        this.dialingMode = dialingMode;
    }

    public List<FreeBusyInfo> getFreeBusyInfoList()
    {
        return freeBusyInfoList;
    }

    public void setFreeBusyInfoList(List<FreeBusyInfo> freeBusyInfoList)
    {
        this.freeBusyInfoList = freeBusyInfoList;
    }

    public TerminalServiceItf getTerminalService() throws SDKException
    {
        if (null == terminalService)
        {
            terminalService = getTerminalClient(this.uri);
        }

        return terminalService;
    }

    public String getModel()
    {
        return model;
    }

    public void setModel(String model)
    {
        this.model = model;
    }

    public String getLisence()
    {
        return lisence;
    }

    public void setLisence(String lisence)
    {
        this.lisence = lisence;
    }

    public String getSoftVersion()
    {
        return softVersion;
    }

    public void setSoftVersion(String softVersion)
    {
        this.softVersion = softVersion;
    }

    public String getHardVersion()
    {
        return hardVersion;
    }

    public void setHardVersion(String hardVersion)
    {
        this.hardVersion = hardVersion;
    }

    public String getLogicVersion()
    {
        return logicVersion;
    }

    public void setLogicVersion(String logicVersion)
    {
        this.logicVersion = logicVersion;
    }

    public Map<String, String> getMicVersion()
    {
        return micVersion;
    }

    public void setMicVersion(Map<String, String> micVersion)
    {
        this.micVersion = micVersion;
    }

    public static IDeviceManager getDevicemanager()
    {
        return deviceManager;
    }

    public String getFrom()
    {
        return from;
    }

    public void setFrom(String from)
    {
        this.from = from;
    }

    public Integer getIsLocalVideoOpen()
    {
        return isLocalVideoOpen;
    }

    public void setIsLocalVideoOpen(Integer isLocalVideoOpen)
    {
        this.isLocalVideoOpen = isLocalVideoOpen;
    }

    private TerminalServiceItf getTerminalClient(String uri) throws SDKException
    {
        TerminalServiceItf terminalService = null;
        try
        {
            Class<?>[] interfaces = new Class<?>[1];
            interfaces[0] = Class
                    .forName("com.huawei.esdk.tp.device.terminal.TerminalServiceItf");

            SessionManager sessionMgr = SessionManager.getInstance();
            MessageContext mc = ThreadLocalHolder.get();
            String sdkSession = (String) mc.getEntities().get(
                    InfoKeys.SDK_SESSION_ID.toString());
            sessionMgr.getSDKSessionAvoidNull(sdkSession).getTerminalUriList().add(uri);
            
            terminalService = (TerminalServiceItf) deviceManager.getDeviceServiceProxy(
                    uri, interfaces);
        }
        catch (ClassNotFoundException e)
        {
            LOGGER.error("", e);
            SDKException ex = new SDKException(e);
            ex.setSdkErrCode(ESDKErrorCodeConstant.ERROR_CODE_SYS_ERROR);
            throw ex;
        }
        catch (SDKException e)
        {
            LOGGER.error("", e);
            throw e;
        }
        
        return terminalService;
    }
    
    //用于OA插件集成，获取所有exchange会场
    public SDKResult<List<Site>> queryExchangeSites()
    		throws SDKException
    {
    	IADCapability adCapability = deviceManager
                .getDeviceServiceProxy(TPConstant.DEV_ID_AD,
                        IADCapability.class);
    	
    	return adCapability.queryExchangeSites();
    }
    
    
    //用于OA插件集成，从exchange上获取会场号码 --- added by jiliang  20140801
    public SDKResult<List<Site>> querySitesFromEx(List<String> userAddress)
    		throws SDKException
    {
    	IADCapability adCapability = deviceManager
                .getDeviceServiceProxy(TPConstant.DEV_ID_AD,
                        IADCapability.class);
    	
    	return adCapability.queryExchangeSitesByUser(userAddress);
    }

    public Integer getVideoFormat()
    {
        return videoFormat;
    }

    public void setVideoFormat(Integer videoFormat)
    {
        this.videoFormat = videoFormat;
    }

    public Integer getVideoProtocol()
    {
        return videoProtocol;
    }

    public void setVideoProtocol(Integer videoProtocol)
    {
        this.videoProtocol = videoProtocol;
    }

    public SDKErrorCode addSiteInfoEx() throws SDKException
    {
        SMC20V1R3C10ISiteManagerCapability siteManager = deviceManager
            .getDeviceServiceProxy(TPConstant.DEV_ID_SMC,
                SMC20V1R3C10ISiteManagerCapability.class);
        
        return siteManager.addSiteInfoEx(this);
    }

    public SDKErrorCode editSiteInfoEx() throws SDKException
    {
        SMC20V1R3C10ISiteManagerCapability siteManager = deviceManager
            .getDeviceServiceProxy(TPConstant.DEV_ID_SMC,
                SMC20V1R3C10ISiteManagerCapability.class);
        return siteManager.editSiteInfoEx(this);
    }

    public String getRegUser()
    {
        return regUser;
    }

    public void setRegUser(String regUser)
    {
        this.regUser = regUser;
    }

    public String getRegPassword()
    {
        return regPassword;
    }

    public void setRegPassword(String regPassword)
    {
        this.regPassword = regPassword;
    }

    public List<VideoCapbilityItem> getVideoCapbilitys()
    {
        if (null == videoCapbilitys)
        {
            videoCapbilitys = new ArrayList<VideoCapbilityItem>();
        }
        
        return videoCapbilitys;
    }

    public void setVideoCapbilitys(List<VideoCapbilityItem> videoCapbilitys)
    {
        this.videoCapbilitys = videoCapbilitys;
    }
    
}
